<?php defined('SYSPATH') or die('No direct script access.');
/*
 * Продукты
 */
class Controller_Admin_Products extends Controller_Admin {

    public function before() {
        parent::before();

        // Вывод в шаблон
        $this->template->submenu = funcwidget::load('menuproducts');
    }

    public function action_index() {

        $products = ORM::factory('product')->find_all();

        $content = View::factory('admin/products/v_products_index', array(
            'products' => $products,
        ));

        // Вывод в шаблон
        $this->template->page_title = 'Товари';
        $this->template->block_center = array($content);
    }


    public function action_add() {

        $prodGroups = ORM::factory('prodGroup')->find_all()->as_array();
        $subGroups = ORM::factory('subGroup')->find_all()->as_array();
        $brands = ORM::factory('brand')->find_all()->as_array();
        
        

        if (isset($_POST['submit'])) {
            $group_id = Arr::get($_POST, 'group_id');
            $sub_id = Arr::get($_POST, 'sub_id');
            $brand_id = Arr::get($_POST, 'brand_id');
            $name = Arr::get($_POST, 'name');
            $price = Arr::get($_POST, 'price');
            $about1 = Arr::get($_POST, 'about1');
            
          

            
            $products = ORM::factory('product')
                        ->set('prodult_id', $group_id)//група товарів
                        ->set('catalog_id', $sub_id)//підгрупа товарів
                        ->set('company_id', $brand_id)//компанія
                        ->set('produkt_name', $name)//назва товару 
                        ->set('product_price',$price);//Ціна
             
                       
            

            try {
                $products->save();
                
                
                $about= ORM::factory('about')
                    ->set('id_number', $products->id)
                    ->set('about', $about1);//опис товару   
                $about->save();
                
                
                
                //header('admin/products');
                //$this->request->redirect('admin/products');
      
                HTTP::redirect('admin/products');
            }
            catch (ORM_Validation_Exception $e) {
                $errors = $e->errors('validation');
            }
        }

        $content = View::factory('admin/products/v_products_add')
                ->bind('errors', $errors)
                
                ->bind('prodGroups', $prodGroups)
                ->bind('group_id', $group_id)
                
                ->bind('subGroups', $subGroups)
                ->bind('sub_id', $sub_id)
                
                ->bind('brands', $brands)
                ->bind('brand_id', $brand_id)
                
                ->bind('name', $name)
                ->bind('price', $price)
                ->bind('about1', $about1);

        // Вывод в шаблон
        $this->template->page_title = 'Додати товар';
        $this->template->block_center = array($content);
    }

    
    
    public function action_edit() {
        $id = (int) $this->request->param('id');


        $prodGroups = ORM::factory('prodGroup')->find_all()->as_array();
        $subGroups = ORM::factory('subGroup')->find_all()->as_array();
        $brands = ORM::factory('brand')->find_all()->as_array();
        
        $products = ORM::factory('product',$id);
            $group_id = $products->prodult_id;
            $sub_id = $products->catalog_id;
            $brand_id =$products->company_id;
            $name = $products->produkt_name;
            $price = $products->product_price;
            $about1 = $products->about->about;
        

        if (isset($_POST['submit'])) {
            $group_id = Arr::get($_POST, 'group_id');
            $sub_id = Arr::get($_POST, 'sub_id');
            $brand_id = Arr::get($_POST, 'brand_id');
            $name = Arr::get($_POST, 'name');
            $price = Arr::get($_POST, 'price');
            $about1 = Arr::get($_POST, 'about1');
            
          

           

            
            $products = ORM::factory('product',$id)
                        ->set('prodult_id', $group_id)//група товарів
                        ->set('catalog_id', $sub_id)//підгрупа товарів
                        ->set('company_id', $brand_id)//компанія
                        ->set('produkt_name', $name)//назва товару 
                        ->set('product_price',$price);//Ціна
            
            $about= ORM::factory('about',$id)
                    ->set('about', $about1);//опис товару 
                       
            

            try {
                $products->save();
                $about->save();
                
                
                
                //header('admin/products');
                //$this->request->redirect('admin/products');
      
                HTTP::redirect('admin/products');
            }
            catch (ORM_Validation_Exception $e) {
                $errors = $e->errors('validation');
            }
        }

        $content = View::factory('admin/products/v_products_edit')
                ->bind('id', $id)
                ->bind('errors', $errors)
                
                ->bind('prodGroups', $prodGroups)
                ->bind('group_id', $group_id)
                
                ->bind('subGroups', $subGroups)
                ->bind('sub_id', $sub_id)
                
                ->bind('brands', $brands)
                ->bind('brand_id', $brand_id)
                
                ->bind('name', $name)
                ->bind('price', $price)
                ->bind('about1', $about1);

        // Вывод в шаблон
        $this->template->page_title = 'Редагувати товар';
        $this->template->block_center = array($content);
    }


     public function action_delete() {
        $id = (int) $this->request->param('id');

       

        $pages = ORM::factory('product', $id);
        $pages->delete();
        $about = ORM::factory('about',$id);
        $about->delete();
        //$this->request->redirect('admin/products');
        HTTP::redirect('admin/products');
    }


    
}